package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specService;

    /**
     * 根据cid查询规格参数的分组数据
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupsByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> groups=specService.querySpecGroupsByCid(cid);
        if(CollectionUtils.isEmpty(groups)){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(groups);
    }

    /**
     * 保存规格参数的分组数据
     * @param specGroups
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveSpecGroups(@RequestBody SpecGroup specGroups){
        specService.saveSpecGroups(specGroups);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改规格参数的分组数据
     * @param specGroups
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroups(@RequestBody SpecGroup specGroups){
        specService.updateSpecGroups(specGroups);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除规格参数的分组数据
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteSpecGroups(@PathVariable("id") Long id){
        specService.deleteSpecGroups(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
     * 新增规格参数分组的参数数据
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParam specParam){
        specService.saveSpecParam(specParam);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 修改规格参数分组的参数数据
     *
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam){
        specService.updateSpecParam(specParam);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
     * 通过id删除该规格参数的数据
     * @param id
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id") Long id){
        specService.deleteSpecParam(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
     * 通过groupId查询该分组的规格参数数据
     *
     * @param gid
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> querySpecParams(
            @RequestParam(value = "gid",required = false) Long gid, @RequestParam(value="cid", required = false) Long cid,
            @RequestParam(value="searching", required = false) Boolean searching, @RequestParam(value="generic", required = false) Boolean generic){

        List<SpecParam> params=specService.querySpecParams(gid,cid,searching,generic);
        if(CollectionUtils.isEmpty(params)){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(params);
    }

    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupWithParamByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> list=specService.queryGroupWithParamByCid(cid);
        if(CollectionUtils.isEmpty(list)){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

}
